package com.atguigu.tingshu.order.pattern.strategy;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.pattern.AbstractTradeOrderStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.atguigu.tingshu.common.constant.SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT;

/**
 * 商品类别为专辑：策略实现类
 * @author: atguigu
 * @create: 2025-03-25 16:24
 */
@Component("1001")
public class AlbumTradeOrderStrategy extends AbstractTradeOrderStrategy {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private AlbumFeignClient albumFeignClient;
    /**
     * 处理结算商品类别为：专辑
     * @param tradeVo
     * @return
     */
    @Override
    public void handleTrade(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        //3. 处理购买商品类别：专辑
        //3.1 远程调用"用户服务"判断是否重复购买专辑 如果是：业务终止
        Long albumId = tradeVo.getItemId();
        Boolean isPaid = userFeignClient.isPaidAlbum(albumId).getData();
        Assert.state(!isPaid, "用户已购买专辑：{}", albumId);

        //3.2 远程调用"专辑服务"获取专辑信息
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "专辑:{}不存在", albumId);
        //3.2.1 专辑价格
        BigDecimal price = albumInfo.getPrice();
        //3.2.2 普通用户折扣
        BigDecimal discount = albumInfo.getDiscount();
        //3.2.3 VIP用户折扣
        BigDecimal vipDiscount = albumInfo.getVipDiscount();

        //3.3 远程调用"用户服务"判断用户身份
        Boolean isVIP = false;
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
        Assert.notNull(userInfoVo, "用户:{}不存在", userId);
        if (userInfoVo.getIsVip().intValue() == 1 && userInfoVo.getVipExpireTime().after(new Date())) {
            isVIP = true;
        }
        //3.4 计算相关价格
        //3.4.1 暂时让订单价等于原价
        BigDecimal originalAmount = price;
        BigDecimal orderAmount = originalAmount;
        BigDecimal derateAmount = new BigDecimal("0.00");
        //3.4.2 如果是普通用户且专辑设置普通用户折扣，则订单价=原价*普通用户折扣
        if (!isVIP && discount.doubleValue() != -1) {
            orderAmount =
                    originalAmount.multiply(discount).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
            derateAmount = originalAmount.subtract(orderAmount);
        }
        //3.4.3 如果是VIP用户且专辑设置VIP用户折扣，则订单价=原价*VIP用户折扣
        if (isVIP && vipDiscount.doubleValue() != -1) {
            orderAmount =
                    originalAmount.multiply(vipDiscount).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
            derateAmount = originalAmount.subtract(orderAmount);
        }
        //3.5 封装订单明细列表、优惠列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(albumId);
        orderDetailVo.setItemName("专辑：" + albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        //3.6 封装订单优惠列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();

        if (originalAmount.compareTo(orderAmount) == 1) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("专辑限时减免：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        //5.2 为商品列表、优惠列表赋值
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //5.3 为订单VO其他杂项赋值：购买商品类别
        orderInfoVo.setItemType(tradeVo.getItemType());
        //5.4 流水号、时间戳及签名
        this.generateTradeNo(orderInfoVo);
        this.generateSign(orderInfoVo);
    }
}
